using Front._1117_2;
using MySqlConnector;

namespace Front._1127;

public partial class login : Form
{
    public login()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        

    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        string sql = "SELECT * FROM admin_info WHERE admin_account = @account and admin_password = @pwd;";
        using var command = new MySqlCommand(sql, connection);
        //获取账号密码
        string account = this.textBox1.Text;
        string password = this.textBox2.Text;
        //加密
        string pwd = MD5Helper.GetMD5(password);
        
        //传参
        command.Parameters.AddWithValue("account", account);
        command.Parameters.AddWithValue("pwd", pwd);
        
        
        using var reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            //成功
            MainForm mainForm = new MainForm();
            mainForm.Show();
            //隐藏登录窗体
            this.Hide();
        }
        else
        {
            //失败
            MessageBox.Show("账号密码错误");
            this.textBox1.Text = "";
            this.textBox2.Text = "";
        }
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        button1 = new System.Windows.Forms.Button();
        textBox1 = new System.Windows.Forms.TextBox();
        textBox2 = new System.Windows.Forms.TextBox();
        SuspendLayout();
        // 
        // button1
        // 
        button1.Location = new System.Drawing.Point(246, 405);
        button1.Name = "button1";
        button1.Size = new System.Drawing.Size(253, 76);
        button1.TabIndex = 0;
        button1.Text = "登录";
        button1.UseVisualStyleBackColor = true;
        button1.Click += button1_Click_1;
        // 
        // textBox1
        // 
        textBox1.Location = new System.Drawing.Point(141, 227);
        textBox1.Name = "textBox1";
        textBox1.Size = new System.Drawing.Size(452, 27);
        textBox1.TabIndex = 1;
        // 
        // textBox2
        // 
        textBox2.Location = new System.Drawing.Point(141, 290);
        textBox2.Name = "textBox2";
        textBox2.Size = new System.Drawing.Size(452, 27);
        textBox2.TabIndex = 1;
        // 
        // login
        // 
        ClientSize = new System.Drawing.Size(723, 673);
        Controls.Add(textBox2);
        Controls.Add(textBox1);
        Controls.Add(button1);
        Load += login_Load;
        ResumeLayout(false);
        PerformLayout();
    }

    private System.Windows.Forms.TextBox textBox3;

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;

    private System.Windows.Forms.Button button1;

    private void login_Load(object sender, EventArgs e)
    {
        this.textBox1.Text = "admin_super01";
        this.textBox2.Text = "123456";
    }
}